using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveBackandForward : MonoBehaviour
{
    public Transform leftPoint, rightPoint;
    public bool isLeft=false,isRight=true;

    public float speed=10f;
    public GameObject movePoint;
    [SerializeField] float maxDistance = 10f;//最大发射距离
    [SerializeField] float grappleSpeed = 10f;//抓取后移动速度
    [SerializeField] LayerMask grappableMask;//获取抓取点的物理图层
    private GameObject player;
    LineRenderer lineRenderer;


    private void Start()
    {
       
        //leftPoint = GameObject.FindGameObjectWithTag("leftPoint").GetComponent<Transform>();
        //rightPoint = GameObject.FindGameObjectWithTag("rightPoint").GetComponent<Transform>();
        
    }
    // Update is called once per frame
    void Update()
    {

        if (isLeft)
        {
            transform.position = Vector2.MoveTowards(transform.position, leftPoint.position, speed * Time.deltaTime);
            if(transform.position==leftPoint.position){isLeft = false;isRight = true;}
        }
        else if (isRight) 
        {
            transform.position = Vector2.MoveTowards(transform.position, rightPoint.position, speed * Time.deltaTime);
            if(transform.position==rightPoint.position){isLeft = true;isRight = false;}

        }

        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, maxDistance, grappableMask);//确定抓取点坐标
        if (hit.collider == null)//当没有抓其他点的时候跟随此物块
        {
            player.transform.position = transform.position;
            

        }
        


    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        GameObject otherObject = collision.gameObject; // 获取碰撞的对象

        if (otherObject.CompareTag("Player")) // 检查碰撞的对象是否为"Player"
        {
            player = otherObject;
            
        }
    }
}
